package com.tripupdater.command
{
	import com.tripupdater.model.Model;
	import com.tripupdater.model.Params;
	import com.tripupdater.vo.CalendarPlaceVO;
	import com.turner.caf.commands.ICommand;
	import com.turner.caf.control.CAFEvent;
	
	import flash.events.Event;
	import flash.net.URLLoader;
	import flash.net.URLRequest;

	public class LoadCalendarCommand implements ICommand
	{
		private var model:Model;
		private var viewTarget:LoadAppDataCommand;
		
		public function LoadCalendarCommand()
		{
			model= Model.getInstance();
		}

		public function execute(event:CAFEvent):void
		{
			viewTarget= event.viewTarget as LoadAppDataCommand;
			var loader:URLLoader= new URLLoader();
			loader.addEventListener(Event.COMPLETE, onComplete);
			loader.load(new URLRequest(Params.HOST + "xml/calendar.xml"));
		}
		
		private function onComplete(e:Event):void
		{
			var xml:XML;
			try{
				xml= new XML(e.target.data);
			} catch(e:Error){ }
			
			model.calendarList= new Array();
			for each(var place:XML in xml.place)
			{
				var pl:CalendarPlaceVO= new CalendarPlaceVO();
				pl.city= 		place.city.toString();
				pl.country= 	place.country.toString();
				pl.date= 		new Date(place.date.toString());
				pl.location= 	place.location.toString();
				
				model.calendarList.push(pl);
			}
			
			viewTarget.onCalendarLoaded();
		}
	}
}